package com.swz.dcrm.adpter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;

import com.swz.dcrm.R;
import com.swz.dcrm.base.ViewHolder;
import com.swz.dcrm.model.Approval;

import java.util.List;

public class ApprovalAdapter extends CustomAdapter<Approval> {

    private boolean isManager;
    private OnButtonClickListener onButtonClickListener;
    Drawable drawableSuccess;
    Drawable drawableFail;
    public interface  OnButtonClickListener{

        void onApproval(Approval approval);
        void onItemClick(Approval approval);
        void onQuotation(Approval approval);
        void onBooking(Approval approval);
    }
    public ApprovalAdapter(Context context,  List<Approval> datas,boolean isManager) {
        super(context, R.layout.item_approval, datas);
        drawableSuccess=mContext.getResources().getDrawable(R.mipmap.success_reason);
        drawableFail= mContext.getResources().getDrawable(R.mipmap.fail_reason);
        drawableFail.setBounds(0,0,drawableFail.getMinimumWidth(),drawableFail.getMinimumHeight());
        drawableSuccess.setBounds(0,0,drawableSuccess.getMinimumWidth(),drawableSuccess.getMinimumHeight());

        this.isManager=isManager;
    }
    @Override
    protected void convert(ViewHolder holder, Approval approval, int position) {








        if(approval.getHandleResult()!=null){
            holder.getView(R.id.tv_pass).setVisibility(View.VISIBLE);

            if(approval.getHandleResult()==1){
                holder.getView(R.id.tv_pass).setEnabled(true);
                holder.setText(R.id.tv_pass,mContext.getString(R.string.item_pass));
            }else {
                holder.setText(R.id.tv_pass,mContext.getString(R.string.item_return));
                holder.getView(R.id.tv_pass).setEnabled(false);
            }


        }else {


            holder.getView(R.id.tv_pass).setVisibility(View.GONE);
        }

        if(isManager){
            holder.setText(R.id.tv_desc,mContext.getString(R.string.approval_desc,approval.getCounselorName(),approval.getApplyName()));
            if(approval.getHandleResult()==null&&approval.getApplyType()==3){
                holder.getView(R.id.btn_quotation).setVisibility(View.VISIBLE);
            }
            if(approval.getHandleResult()!=null){
                holder.getView(R.id.btn_approval).setVisibility(View.GONE);
            }
        }else {
            holder.getView(R.id.btn_approval).setVisibility(View.GONE);
            if(approval.getHandleResult()!=null){

                TextView textView=holder.getView(R.id.tv_approval_opinion);
                if(approval.getHandleResult()==1){
                    textView .setCompoundDrawables(drawableSuccess, null, null, null);
                }else {
                    textView .setCompoundDrawables(drawableFail, null, null, null);
                }
                holder.getView(R.id.tv_approval_opinion).setVisibility(View.VISIBLE);
                holder.setText(R.id.tv_approval_opinion,approval.getHandleComment());
                if(approval.getApplyType()==3){
                    holder.getView(R.id.ll).setVisibility(View.VISIBLE);
                    if(approval.getHandleResult()!=null&&approval.getHandleResult()==1){
                        holder.getView(R.id.btn_look_quotation).setVisibility(View.VISIBLE);
                        holder.getView(R.id.btn_booking).setVisibility(View.VISIBLE);
                    }else if(approval.getHandleResult()!=null&&approval.getHandleResult()==0){
                        holder.getView(R.id.btn_look_quotation).setVisibility(View.VISIBLE);
                        holder.getView(R.id.btn_booking).setVisibility(View.GONE);
                    }
                }else {
                    holder.getView(R.id.ll).setVisibility(View.GONE);
                }
                holder.getView(R.id.tv_approval_date).setVisibility(View.GONE);

            }
            holder.setText(R.id.tv_desc,mContext.getString(R.string.approval_desc1,approval.getApplyName()));
        }

        holder.setText(R.id.tv_customer_name,approval.getCustomerName()+"");
        holder.setText(R.id.tv_car_model,approval.getCarModelName()+"");
        holder.setText(R.id.tv_approval_date,mContext.getString(R.string.approval_time,approval.getCreateTime()));
        holder.setOnClickListener(R.id.btn_approval,v -> {
           if(onButtonClickListener!=null){
               onButtonClickListener.onApproval(approval);
           }
        });
        holder.setOnClickListener(R.id.btn_quotation,v -> {
            if(onButtonClickListener!=null){
                onButtonClickListener.onQuotation(approval);
            }
        });
        holder.setOnClickListener(R.id.btn_look_quotation,v -> {
            if(onButtonClickListener!=null){
                onButtonClickListener.onQuotation(approval);
            }
        });
        holder.setOnClickListener(R.id.btn_booking,v -> {
            if(onButtonClickListener!=null){
             onButtonClickListener.onBooking(approval);
            }
        });
        holder.itemView.setOnClickListener(v -> {
            if(onButtonClickListener!=null){
                onButtonClickListener.onItemClick(approval);
            }
        });
    }

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }
}
